package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 请求URL：http://localhost:8080/supplier/getComboboxList
     * 请求参数：String q（供应商名称模糊查询）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：List<Supplier>
     */
    @PostMapping("supplier/getComboboxList")
    public List<Supplier> supplierComboboxList(String q) {
        return purchaseService.supplierComboboxList(q);
    }

    /**
     * http://localhost:8080/purchaseListGoods/save?purchaseNumber=JH1605768306735（进货单号前端生成）
     * 请求参数：PurchaseList purchaseList, String purchaseListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("purchaseListGoods/save")
    public ServiceVO savePurchaseListGoods(PurchaseList purchaseList, String purchaseListGoodsStr) {
        try {
            List<PurchaseListGoods> list = JSON.parseArray(purchaseListGoodsStr, PurchaseListGoods.class);
            purchaseService.savePurchaseListGoods(purchaseList, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败");
        }
        return new ServiceVO(100, "请求成功!");
    }

    /**
     * http://localhost:8080/returnListGoods/save?returnNumber=TH1605768591211（退货单号前端生成）
     * 请求参数：ReturnList returnList, String returnListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("returnListGoods/save")
    public ServiceVO saveReturnListGoods(ReturnList returnList, String returnListGoodsStr) {
        try {
            List<ReturnListGoods> list = JSON.parseArray(returnListGoodsStr, ReturnListGoods.class);
            purchaseService.saveReturnListGoods(returnList, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败");
        }
        return new ServiceVO(100, "请求成功!");
    }

    /**
     * 请求URL：http://localhost:8080/purchaseListGoods/list
     * 请求参数：String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("purchaseListGoods/list")
    public Map<String, Object> purchaseListGoodsList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        return purchaseService.purchaseListGoodsList(purchaseNumber, supplierId, state, sTime, eTime);
    }

    /**
     * 请求URL：http://localhost:8080/purchaseListGoods/goodsList
     * 请求参数：Integer purchaseListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("purchaseListGoods/goodsList")
    public Map<String, Object> purchaseListGoodsGetGoodss(Integer purchaseListId) {
        return purchaseService.purchaseListGoodsGetGoodss(purchaseListId);
    }

    /**
     * 请求URL：http://localhost:8080/purchaseListGoods/delete
     * 请求参数：Integer purchaseListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("purchaseListGoods/delete")
    public ServiceVO delPurchaseListGoods(Integer purchaseListId) {
        try {
            purchaseService.delPurchaseListGoods(purchaseListId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!!");
        }
        return new ServiceVO(100, "请求成功!!");

    }

    /**
     * 请求URL：http://localhost:8080/returnListGoods/list
     * 请求参数：String returnNumber,Integer supplierId, Integer state,String sTime,String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("returnListGoods/list")
    public Map<String, Object> returnListGoodsList(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        return purchaseService.returnListGoodsList(returnNumber, supplierId, state, sTime, eTime);
    }

    /**
     * 请求URL：http://localhost:8080/returnListGoods/goodsList
     * 请求参数：Integer returnListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("returnListGoods/goodsList")
    public Map<String, Object> returnListGoodsGetGoodss(Integer returnListId) {
        return purchaseService.returnListGoodsGetGoodss(returnListId);
    }

    /**
     * 请求URL：http://localhost:8080/returnListGoods/delete
     * 请求参数：Integer returnListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("returnListGoods/delete")
    public ServiceVO delReturnListGoods(Integer returnListId) {
        try {
            purchaseService.delReturnListGoods(returnListId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!!");
        }
        return new ServiceVO(100, "请求成功!!");
    }
    /**
     * 请求URL：http://localhost:8080/purchaseListGoods/updateState
     * 请求参数：Integer purchaseListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("purchaseListGoods/updateState")
    public ServiceVO updateState(Integer purchaseListId){
        try {
            purchaseService.updateState(purchaseListId);
        } catch (Exception e) {
            e.printStackTrace();return new ServiceVO(200, "请求失败!!");
        }
        return new ServiceVO(100, "请求成功!!");
    }
    /**
     * 请求URL：http://localhost:8080/purchaseListGoods/count
     * 请求参数：String sTime, String eTime ,Integer goodsTypeId, String codeOrName
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String
     */
    @PostMapping("purchaseListGoods/count")
    public List<Map> count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        return purchaseService.count(sTime,eTime,goodsTypeId,codeOrName);
    }
}

